// SlagCar/Login/Login.js
// Logistics/Logins/Logins.js
import {config} from "../../utils/config";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: wx.getStorageSync('avatar'),             // 头像

    system: [],
    systemIndex: 0,
    role: [
      {name: '司机',id: 'driver'},
      {name: '管理员',id: ''},
    ],
    roleIndex: 0
  },

  // methods
  systemChange(e) {    // 选择系统
    this.setData({
      systemIndex: e.detail.value,
    })
  },
  roleChange(e) {    // 选择角色
    this.setData({
      roleIndex: e.detail.value,
    })
  },
  formSubmit(e) {   // form提交按钮
    let formData = e.detail.value,
      flag = false,
      keyArr = {
        system_id: '系统',
        User: '账号',
        Password: '密码',
      }
    flag = Object.keys(keyArr).some((key, i) => {
      if(!formData[key]) {
        this._showToast(`请填写${keyArr[key]}！`)
        return true
      }
    })
    if(flag) return;
    wx.showLoading({mask: true})
    let system_type = formData.User_type === 'driver' ? 'ZT' : 'DIY';
    // 提交
    let post = {system_type,...formData};
    this.postLogin(post).then(res => {
      wx.hideLoading();
      let {driver_row_id,driver_id,driver_name,driver_car_row_id,driver_car_id,driver_car_name,system_id} = res.data.row[0] || {},
        {avatar} = this.data;
      // 存储登录信息
      wx.$util.setLoginStorage(res.data.row[0]);
      let storageObj = {
        loginAcct: formData.User,
        loginPwd: formData.Password,
        user_type: formData.User_type,
        avatar,
        system_type: 'ZT'
      }
      if (formData.User_type === 'driver'){
        storageObj = {...storageObj,driver_row_id,driver_id,driver_name,driver_car_row_id,driver_car_id,driver_car_name}
      }
      Object.entries(storageObj).forEach(([key, val]) => wx.setStorageSync(key, val))

      wx.$getBarTitle(system_id);
      // 跳转到首页
      wx.reLaunch({url: '../Index/Index'});
    })
  },

  postLogin(post){
    return new Promise(resolve => {
      wx.showLoading({ mask: true, });
      wx.$post(1,'Authorize/Token_miniprogram',post).then(res => {
        if (res.errcode != 0) return;
        resolve(res);
      })
    })
  },


  getPTConnection(){
    let url = 'ZT/ZTConnectionString';
    wx.$get(5,url).then(res => {
      if (res.errcode !== 0) return;
      let system = res.data.row || [],
        obj = {
          system,
        },
        system_id = wx.getStorageSync('system_id'),
        his = -1;
      system_id && (his = system.findIndex(item =>{ return item.system_id == system_id}));
      // 获取历史记录填充
      if (his >= 0) {
        obj.systemIndex = his
      }
      this.setData(obj)
    })
  },

  _showToast(content) {
    wx.showToast({
      title: content,
      icon: 'none'
    })
  },

  //切换系统到diy
  toggleSys(){
    wx.redirectTo({url: '/pages/login/login'})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPTConnection();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
